package com.wei800.turnover.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei800.common.action.abs.AbstractBaseAction;
import com.wei800.common.http.RequestData;
import com.wei800.common.http.ResponseData;
import com.wei800.common.util.bean.DeepBeanUtils;
import com.wei800.dto.KuaiDiBoxOrderDTO;
import com.wei800.dto.MemberDTO;
import com.wei800.service.IKuaiDiBoxOrderService;
import com.wei800.turnover.entity.KuaiDiBoxOrder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/zzx/kuaidiboxorder")
@Slf4j
public class KuaiDiBoxOrderAction extends AbstractBaseAction { // 必须要继承父类
    @Autowired
    private IKuaiDiBoxOrderService<KuaiDiBoxOrder> kuaiDiBoxOrderService;

    @PostMapping("add")
    public Object add(@RequestBody RequestData<KuaiDiBoxOrderDTO> data) {
        log.info("增加周转箱数据，{}", data);
        ResponseData result = new ResponseData();
        result.setRespCode(HttpStatus.OK.value());
        result.setRespData(this.kuaiDiBoxOrderService.add(data.getReqData())); // HTTP响应内容
        return result;
    }
    @PostMapping("edit")
    public Object edit(@RequestBody RequestData<KuaiDiBoxOrderDTO> data) {
        log.info("更新周转箱数据，{}", data);
        ResponseData result = new ResponseData();
        result.setRespCode(HttpStatus.OK.value());
        KuaiDiBoxOrder entity = new KuaiDiBoxOrder();
        DeepBeanUtils.copyProperties(data.getReqData(), entity);
        QueryWrapper<KuaiDiBoxOrder> wrapper = new QueryWrapper<>(); 	// 条件构造器
        wrapper.eq("id", entity.getId());
        result.setRespData(this.kuaiDiBoxOrderService.update(entity, wrapper)); // HTTP响应内容
        return result;
    }
    @PostMapping("get")
    public Object get(@RequestBody RequestData<Long> data) {
        log.info("查询周转箱数据，{}", data);
        ResponseData result = new ResponseData();
        result.setRespCode(HttpStatus.OK.value());
        KuaiDiBoxOrderDTO dto = new KuaiDiBoxOrderDTO();
        DeepBeanUtils.copyProperties(this.kuaiDiBoxOrderService.getById(data.getReqData()), dto);
        result.setRespData(dto); // HTTP响应内容
        return result;
    }
    @PostMapping("list")
    public Object list(@RequestBody RequestData<Map<String, Object>> data) {
        log.info("查询周转箱数据，{}", data);
        ResponseData result = new ResponseData();
        result.setRespCode(HttpStatus.OK.value());
        IPage<KuaiDiBoxOrder> page = new Page<>(
                (Long)data.getReqData().get("cp"),
                (Long)data.getReqData().get("ls"), true); // 包装分页数据

        IPage listResult = this.kuaiDiBoxOrderService.page(page);
        Map<String, Object> map = new HashMap<>();
        map.put("pages", listResult.getPages()); // 总页数
        map.put("total", listResult.getTotal()); // 总记录数
        List<MemberDTO> boxData = DeepBeanUtils.copyListProperties(listResult.getRecords(), KuaiDiBoxOrderDTO::new);
        result.setRespData(boxData); // HTTP响应内容
        return result;
    }
}
